<template>
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu
          mode="inline"
          @click="handleClick"
          :style="{ height: '100%', borderRight: 0 }"
      >
        <a-menu-item>At All</a-menu-item>
        <a-sub-menu v-for="item in level" :key="item.id" :value="item.id">
          <template #title>
              <span>
                {{item.name}}
              </span>
          </template>
          <a-menu-item v-for="children in item.children" :key="children.id" :value="children.id">{{children.name}}</a-menu-item>
        </a-sub-menu>

      </a-menu>
    </a-layout-sider>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }">

      <div v-show="showEbookStatistic">
        <EbookStatistic></EbookStatistic>
      </div>


      <a-list v-show="!showEbookStatistic" item-layout="vertical" size="large" :grid="{ gutter: 16, column: 3 }"  :data-source="ebook">

        <template #renderItem="{ item }">

          <a-list-item key="item.name">
<!--            <template #actions>-->
<!--              <span v-for="{ type, text } in actions" :key="type">-->
<!--                <component :is="type" style="margin-right: 8px" />-->
<!--                {{ text }}-->
<!--              </span>-->
<!--            </template>-->

            <template #actions>
             <span>
              <component :is="'LikeOutlined'" />
                {{ item.voteCount }} &nbsp;
              <component :is="'FileOutlined'" />
                {{ item.docCount }}&nbsp;
              <component :is="'UserOutlined'" />
                {{ item.viewCount }}
            </span>
            </template>

            <a-list-item-meta :description="item.description">
              <template #title>
                <router-link :to="'/doc?ebookId='+item.id">{{ item.name }}</router-link>
<!--                <a :href="item.href">{{ item.name }}</a>-->
              </template>
              <template #avatar><a-avatar :src="item.cover" /></template>
            </a-list-item-meta>


          </a-list-item>
        </template>
      </a-list>

    </a-layout-content>
  </a-layout>
</template>

<script>
import axios from "axios"
import {ref, onMounted } from "vue"
import { UserOutlined, LaptopOutlined, NotificationOutlined,StarOutlined, LikeOutlined, MessageOutlined,FileOutlined} from '@ant-design/icons-vue';
import {arrayToTree} from "@/util/Tool";
import EbookStatistic from '@/components/EbookStatistic';
export default {
  name: 'HomeView',
  components: {
    UserOutlined,
    LaptopOutlined,
    NotificationOutlined,
    StarOutlined,
    LikeOutlined,
    MessageOutlined,
    FileOutlined,
    EbookStatistic
  },
  setup(){
    const ebook = ref()

    const level = ref();

    const showEbookStatistic = ref(true);

    function loadCategory(){
      console.log("loadCategory...")
      axios.get("api/categoryList")
          .then(res=>{
            level.value = arrayToTree(res.data.data,0)
          })
    }

    const category2Id = ref()

    const handleClick = (category) =>{
      console.log("handleClick")
      console.log(category.key)
      category2Id.value = category.key
      showEbookStatistic.value = !showEbookStatistic.value
      loadEbook()
    }

    const loadEbook=()=>{
      axios.get("api/ebookList",{
        params: {
          currentPage: 1,
          pageSize: 10,
          category2Id:category2Id.value
        }})
          .then(res=>{
            ebook.value = res.data.data.list
          })
    }

    onMounted(()=>{
      console.log("onMounted HomeView")
      loadCategory()
      loadEbook()
    })

    return{
      ebook,
      level,
      category2Id,
      showEbookStatistic,
      handleClick
    }
  }
}
</script>

<style scoped>
.ant-avatar{
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 10px;
}
</style>